package com.maodou.core.utils.permission

import android.accessibilityservice.AccessibilityService
import android.os.Build
import com.hjq.permissions.XXPermissions
import com.hjq.permissions.permission.PermissionLists
import com.hjq.permissions.permission.base.IPermission
import com.maodou.core.utils.ActivityProvider

/**
 * 全局权限申请配置工具
 */
object PermissionUtils {

    /**
     * 申请相机和存储权限
     * @param onGranted 权限授予回调
     */
    fun requestCameraAndStoragePermissions(
        onGranted: (Boolean) -> Unit
    ) {
        val permissions = ArrayList<IPermission>()
        permissions.add(PermissionLists.getCameraPermission())
        permissions.add(PermissionLists.getWriteExternalStoragePermission())
        requestPermissions(permissions,onGranted)
    }

    /**
     * 获取媒体文件读写权限
     * @param onGranted 权限授予回调
     */
    fun requestStoragePermissions(
        onGranted: (Boolean) -> Unit
    ) {
        val permissions = ArrayList<IPermission>()
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU) {
            permissions.add(PermissionLists.getManageExternalStoragePermission())
        } else {
            permissions.add(PermissionLists.getReadMediaImagesPermission())
            permissions.add(PermissionLists.getReadMediaVideoPermission())
            permissions.add(PermissionLists.getReadMediaAudioPermission())
            permissions.add(PermissionLists.getReadMediaVisualUserSelectedPermission())
            permissions.add(PermissionLists.getWriteExternalStoragePermission())
        }
        requestPermissions(permissions,onGranted)
    }

    /**
     * 获取所有文件读写权限
     * @param onGranted 权限授予回调
     */
    fun requestReadWritePermissions(
        onGranted: (Boolean) -> Unit
    ) {
        val permissions = ArrayList<IPermission>()
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU) {
            permissions.add(PermissionLists.getManageExternalStoragePermission())
        } else {
//            permissions.add(PermissionLists.getReadExternalStoragePermission())
            permissions.add(PermissionLists.getWriteExternalStoragePermission())
        }
        requestPermissions(permissions,onGranted)
    }

    /**
     * 获取无障碍权限
     * @param onGranted 权限授予回调
     */
    fun requestAccessibilityServicePermissions(
        accessibilityServiceClass: Class<out AccessibilityService?>,
        onGranted: (Boolean) -> Unit
    ) {
        val permissions = ArrayList<IPermission>()
        permissions.add(PermissionLists.getBindAccessibilityServicePermission(accessibilityServiceClass))
        requestPermissions(permissions,onGranted)
    }

    /**
     * 获取App安装列表权限
     */
    fun requestInstalledAppsPermissions(onGranted: (Boolean) -> Unit) {
        val permissions = ArrayList<IPermission>()
        permissions.add(PermissionLists.getGetInstalledAppsPermission())
        requestPermissions(permissions,onGranted)
    }

    /**
     * 获取vpn权限
     */
    fun requestVpnServicePermissions(onGranted: (Boolean) -> Unit) {
        val permissions = ArrayList<IPermission>()
        permissions.add(PermissionLists.getBindVpnServicePermission())
        requestPermissions(permissions,onGranted)
    }

    /**
     * 获取悬浮框权限
     */
    fun requestSystemAlertPermissions(onGranted: (Boolean) -> Unit) {
        val permissions = ArrayList<IPermission>()
        permissions.add(PermissionLists.getSystemAlertWindowPermission())
        requestPermissions(permissions,onGranted)
    }

    /**
     * 获取安装App权限
     */
    fun requestInstallPackagesPermissions(onGranted: (Boolean) -> Unit) {
        val permissions = ArrayList<IPermission>()
        permissions.add(PermissionLists.getRequestInstallPackagesPermission())
        requestPermissions(permissions,onGranted)
    }


    /**
     * 申请指定权限列表
     * @param permissions 权限列表
     * @param onGranted 权限授予回调
     */
    fun requestPermissions(
        permissions: List<IPermission>,
        onGranted: (Boolean) -> Unit
    ) {
        val context = ActivityProvider.requireCurrentActivity()
        val request = XXPermissions.with(context)
            .interceptor(PermissionInterceptor())
            .description(PermissionDescription())

        permissions.forEach { permission ->
            request.permission(permission)
        }

        request.request { _, deniedList ->
            val allGranted = deniedList.isEmpty()
            if (!allGranted) return@request
            onGranted(allGranted)
        }
    }

}